//
//  UIView+Extension.swift
//  TestDemo
//
//  Created by ayin on 2019/5/31.
//  Copyright © 2019 aYin. All rights reserved.
//  UIView扩展

import Foundation

extension UIView {
    /*
     usage: self.view.addDottedLine(UIColor.lightGray, lineWidth: 1.0, lineDashPattern: [4,3])
     */
    /// 添加虚线
    ///
    /// - Parameters:
    ///   - lineColor: 线颜色
    ///   - lineWidth: 线宽
    ///   - lineDashPattern: 线间距（一个数组，定义虚线中实线部分和间隔部分分别的长度）
    public func addDottedLine(_ lineColor: UIColor = UIColor.lightGray, lineWidth: CGFloat = 1.0, lineDashPattern: [NSNumber] = [4,3])  {
        let border             = CAShapeLayer()
        border.strokeColor     = lineColor.cgColor
        border.fillColor       = nil
        let path =  UIBezierPath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: self.width, y: 0))
        
        border.path            = path.cgPath
        border.frame           = self.bounds
        border.lineCap         = CAShapeLayerLineCap(rawValue: "square")
        border.lineWidth       = lineWidth
        border.lineDashPattern = lineDashPattern
        self.layer.addSublayer(border)
    }
    
    //添加虚线边框
    func dottedBorder(_ color: UIColor, width:CGFloat, radius: CGFloat = 0, lineDashPattern: [NSNumber] = [4,3])
    {
        let border             = CAShapeLayer()
        border.strokeColor     = color.cgColor
        border.fillColor       = nil
        border.path            = UIBezierPath(rect: self.bounds).cgPath
        border.frame           = self.bounds
        border.lineCap         = CAShapeLayerLineCap(rawValue: "square")
        border.masksToBounds   = true
        border.cornerRadius    = radius
        border.lineWidth       = width
        border.lineDashPattern = lineDashPattern
        self.layer.addSublayer(border)
    }
    
//    /// 边框
//    func border(_ color: UIColor,width: CGFloat)
//    {
//        self.layer.borderColor = color.cgColor
//        self.layer.borderWidth = width
//    }
//    
//    
//    /// 圆角
//    func cornerRadius(_ radius: CGFloat)
//    {
//        self.layer.masksToBounds = true
//        self.layer.cornerRadius = radius
//    }
}
